package lihao3;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Test2{
    // 读文本文件
    public static String StringreadFrom(File f) throws IOException {

        char cBuf[] = new char[(int) f.length()];
        FileReader fr = new FileReader(f);
        fr.read(cBuf);
        fr.close();
        System.out.println(cBuf);
        return new String(cBuf);
    }

    //写文本文件
    public static void writeToFile(String s, File f) {
        BufferedWriter bw = null;// 缓冲写(过滤流)
        try {
            bw = new BufferedWriter(new FileWriter(f));
            bw.write(s);
        } catch (IOException e) {
        } finally {
            if (bw != null)
                try {
                    bw.close();
                } catch (IOException e) {
                }
        }
    }

    // 拷贝字节流
    public static void fileCopy(File from, File to) {
        BufferedInputStream bis = null;// 缓冲流
        BufferedOutputStream bos = null;
        try {

            bis = new BufferedInputStream(new FileInputStream(from));
            bos = new BufferedOutputStream(new FileOutputStream(to));
            int x;
            while ((x = bis.read()) != -1) {
                bos.write(x);
            }
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } finally {
            try {
                if (bis != null)
                    bis.close();
                if (bos != null)
                    bos.close();
            } catch (IOException e) {
            }
        }
    }

    public static void main(String[] args) throws IOException  {
        Scanner sc=new Scanner(System.in);
        File f=new File("F:\\a.txt");
        File from=new File("F:\\b.txt");
        File to=new File("F:\\c.txt");
        System.out.println("学号，姓名，性别；");
        String s=sc.nextLine();
        StringreadFrom(f);
        writeToFile(s,f);
        fileCopy(from,to);
    }
}
